import { asyncRoutes, constantRoutes } from '@/router'
import Layout from '@/layout'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

//

export function getRouters(routesGet, adStaus) {
    const resRouter = []
    routesGet.forEach((item)=>{
      if(item.meta&&item.meta.roles&&item.meta.roles.length>0){
        if(item.meta.roles.includes(adStaus)){
            if(item.children){
              item.children=getRouters(item.children,adStaus)
              resRouter.push(item)
            }else{
              resRouter.push(item)
            }
        }
      }
  })
  return resRouter
}

                               //总的     后端读的
export function getBackRouters(routesOld, data,) {
        const resRouter = []
        routesOld.forEach(itemOld => {
           data.forEach(item => {
                     if(itemOld.path==item.path){
                          if (itemOld.children && itemOld.children.length>0) {
                            console.log('我执行了吗')
                            itemOld.children=getBackRouters(itemOld.children, item.children)
                          }
                          resRouter.push(itemOld)
                     }          
           })
        })
        return resRouter
}


const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    //开始  在这里进行代码的改写 可以全部注释 
    //最原始的那种admin editor这种方式
    //这个是权限控制的代码是好的
    // return new Promise(resolve => {
    //   let accessedRoutes=[]
    //   let adminStatus="admin"
    //   accessedRoutes=getRouters(asyncRoutes,adminStatus)
    //     commit('SET_ROUTES', accessedRoutes)
    //     resolve(accessedRoutes)
    // })
    //结束

    //第二种方式
    //开始
    //对本地的路由和后端获取的路由进行对比
    //结束


    // return new Promise(resolve => {
    // let getNewRouter=[
    //     {
    //       path: '/permission',
    //       component: '#',
    //       redirect: '/permission/page',
    //       alwaysShow: true, 
    //       name: 'Permission',
    //       meta: {
    //         title: 'Permission',
    //         icon: 'lock',
    //       },
    //       children: [
    //         {
    //             path: 'page',
    //             component: '/permission/page',
    //             name: 'PagePermission',
    //             meta: {
    //             title: 'Page Permission',
               
    //             }
    //         },
    //         {
    //              path: 'directive',
    //             component:'/permission/directive',
    //             name: 'DirectivePermission',
    //             meta: {
    //             title: 'Directive Permission',
               
    //             }
    //         },
           
    //       ]
    //     },
    //     {
    //       path: '/icon',
    //       component: '#',
    //       children: [
    //         {
    //           path: 'index',
    //           component:'/icons/index',
    //           name: 'Icons',
    //           meta: { title: 'Icons', icon: 'icon', noCache: true }
    //         }
    //       ]
    //     },
    // ]

    //   let accessedRoutes=[]
    //   accessedRoutes=getBackRouters(asyncRoutes,getNewRouter)
    //   console.log('lllllllllllllllllllllll')
    //   console.log(accessedRoutes)
    //   console.log('lllllllllllllllllllllll')
    //   // { path: '*', redirect: '/404', hidden: true }
    //   accessedRoutes.push( { path: '*', redirect: '/404', hidden: true } )
    //    commit('SET_ROUTES', accessedRoutes)
    //     resolve(accessedRoutes)
    // })

  //  console.log('yyyyyyyyyyyyyyyyyyyyyyy')

    return new Promise(resolve => {
      let accessedRoutes
      if (roles.includes('admin')) {
        accessedRoutes = asyncRoutes || []
      } else {
        accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      }
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })



  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
